/**
 * Copyright (C) 2013 Daniel Gee.
 * See LICENSE.txt for more details.
 */
package examples;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.List;

import com.rl.dungeon.Location;
import com.rl.dungeon.builders.DefaultBuilder;
import com.rl.dungeon.pathing.HillMap;
import com.rl.gui.Appearance;
import com.rl.gui.Screen;
import com.rl.util.CollectionUtil;
import com.rl.util.DiceRoller;

/**
 * @author Daniel Gee
 */
public class PathFilteredScreen extends Screen
{
	/**
	 * for serialization.
	 */
	private static final long serialVersionUID = 9078946071002115006L;

	/**
	 * shown in place of the real dungeon at any point on the path
	 */
	public static final Appearance pathAppearance = new Appearance('*', Color.blue);

	/**
	 * the dungeon we're putting an overlay over
	 */
	private final SimpleDungeon dungeon;

	/**
	 * the path that's overlaying the dungeon
	 */
	private final List<Location> path;

	/**
	 * @param path
	 * @param dungeon
	 */
	PathFilteredScreen(List<Location> path, SimpleDungeon dungeon)
	{
		super(null);
		this.path = path;
		this.dungeon = dungeon;
	}

	/**
	 * @see com.rl.gui.AppearanceMap#getAppearanceAt(com.rl.dungeon.Location)
	 */
	@Override
	public Appearance getAppearanceAt(Location location)
	{
		if (this.path != null ? this.path.contains(location) : false)
		{
			return PathFilteredScreen.pathAppearance;
		}
		else
		{
			return this.dungeon.getAppearanceAt(location);
		}
	}

	/**
	 * @see com.rl.gui.AppearanceMap#getDisplayStartLocation(int, int)
	 */
	@Override
	public Location getDisplayStartLocation(int displayWidth, int displayHeight)
	{
		return Location.of(0, 0, 0);
	}

	/**
	 * @return the dungeon
	 */
	public SimpleDungeon getDungeon()
	{
		return this.dungeon;
	}

	/**
	 * @return the path
	 */
	public List<Location> getPath()
	{
		return this.path;
	}

	/**
	 * @see com.rl.gui.Screen#respondToKeypress(java.awt.event.KeyEvent)
	 */
	@Override
	public Screen respondToKeypress(KeyEvent key)
	{
		switch (key.getKeyChar())
		{
		case 'n':
			return new PathFilteredScreen(null, new SimpleDungeon(new DefaultBuilder(
					new DiceRoller()).generate(dungeon.getWidth(), dungeon.getHeight())));
		}
		return this;
	}

	/**
	 * @see com.rl.gui.Screen#respondToMouseAt(java.awt.event.MouseEvent, com.rl.dungeon.Location)
	 */
	@Override
	public Screen respondToMouseAt(MouseEvent event, Location location)
	{
		if (event.getID() != MouseEvent.MOUSE_CLICKED)
		{
			return this;
		}
		final HillMap hm = new HillMap(dungeon, dungeon.getEdge(), CollectionUtil.toSet(dungeon
				.getStairs()));
		return new PathFilteredScreen(hm.getPathFrom(location), dungeon);
	}

}
